﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Accounting.DataAccess;
using Accounting.ViewModel;

namespace Accounting.Controllers
{
    public class BillController : Controller
    {
        private accountEntities db = new accountEntities();

        //
        // GET: /Bill/

        public ViewResult Index()
        {
            var bills = db.Bills.Include("SubCategory").Include("Category");
            return View(bills.ToList());
        }


        //
        // GET: /Bill/Create

        public ActionResult Create()
        {
            BillViewModel bill = new BillViewModel();
            bill.Categories = new SelectList(db.Categories, "Id", "Name");
            return View(bill);
        }

        public ActionResult SubCategories(long categoryId)
        {
            var subs = db.SubCategories.Where(s => s.CategoryId == categoryId);

            return Json(
                subs.Select(s => new { Id = s.Id, Name = s.Name }),
                JsonRequestBehavior.AllowGet
                );
        }

        //
        // POST: /Bill/Create

        [HttpPost]
        public ActionResult Create(BillViewModel billViewModel)
        {
            if (ModelState.IsValid)
            {
                Bill bill = billViewModel.ToModel();
                db.Bills.AddObject(bill);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.SubCategoryId = new SelectList(db.SubCategories, "Id", "Name", billViewModel.SubCategoryId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", billViewModel.CategoryId);
            return View(billViewModel);
        }

        //
        // GET: /Bill/Edit/5

        public ActionResult Edit(long id)
        {
            Bill bill = db.Bills.Single(b => b.Id == id);
            BillViewModel billViewModel = new BillViewModel(bill);

            ViewBag.SubCategoryId = new SelectList(db.SubCategories, "Id", "Name", bill.SubCategoryId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", bill.CategoryId);
            return View(billViewModel);
        }

        //
        // POST: /Bill/Edit/5

        [HttpPost]
        public ActionResult Edit(BillViewModel billViewModel)
        {
            if (ModelState.IsValid)
            {
                Bill bill = billViewModel.ToModel();

                db.Bills.Attach(bill);
                db.ObjectStateManager.ChangeObjectState(bill, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.SubCategoryId = new SelectList(db.SubCategories, "Id", "Name", billViewModel.SubCategoryId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", billViewModel.CategoryId);
            return View(billViewModel);
        }

        //
        // GET: /Bill/Delete/5

        public ActionResult Delete(long id)
        {
            Bill bill = db.Bills.Single(b => b.Id == id);
            return View(bill);
        }

        //
        // POST: /Bill/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Bill bill = db.Bills.Single(b => b.Id == id);
            db.Bills.DeleteObject(bill);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}